package com.starsea.book.ui.newpage.normalview.delegate

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.util.AttributeSet
import android.widget.Switch
import android.widget.TextView
import com.starsea.book.R

/**
 * Function: 设置Switch thumb及track相关代理
 * Description:
 */
class RadiusSwitchDelegate  //以上为xml对应属性解析
  (view: TextView?, context: Context?, attrs: AttributeSet?) :
  RadiusCompoundButtonDelegate(view, context, attrs) {
  private var mSwitch: Switch? = null
  private var mStateThumbDrawable: StateListDrawable? = null
  private var mStateTrackDrawable: StateListDrawable? = null

  //以下为xml对应属性解析
  private var mThumbDrawableWidth = 0
  private var mThumbDrawableHeight = 0
  private var mThumbDrawable: Drawable? = null
  private var mThumbPressedDrawable: Drawable? = null
  private var mThumbDisabledDrawable: Drawable? = null
  private var mThumbSelectedDrawable: Drawable? = null
  private var mThumbCheckedDrawable: Drawable? = null
  private var mThumbStrokeColor = 0
  private var mThumbStrokePressedColor = 0
  private var mThumbStrokeDisabledColor = 0
  private var mThumbStrokeSelectedColor = 0
  private var mThumbStrokeCheckedColor = 0
  private var mThumbStrokeWidth = 0
  private var mThumbRadius = 0f
  private var mTrackDrawableWidth = 0
  private var mTrackDrawableHeight = 0
  private var mTrackDrawable: Drawable? = null
  private var mTrackPressedDrawable: Drawable? = null
  private var mTrackDisabledDrawable: Drawable? = null
  private var mTrackSelectedDrawable: Drawable? = null
  private var mTrackCheckedDrawable: Drawable? = null
  private var mTrackStrokeColor = 0
  private var mTrackStrokePressedColor = 0
  private var mTrackStrokeDisabledColor = 0
  private var mTrackStrokeSelectedColor = 0
  private var mTrackStrokeCheckedColor = 0
  private var mTrackStrokeWidth = 0
  private var mTrackRadius = 0f
  override fun initAttributes(context: Context?, attrs: AttributeSet?) {
    mThumbDrawableWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_thumbDrawableWidth, -1)
    mThumbDrawableHeight =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_thumbDrawableHeight, -1)
    mThumbDrawable = mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_thumbDrawable)
    mThumbPressedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_thumbPressedDrawable)
    mThumbDisabledDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_thumbDisabledDrawable)
    mThumbSelectedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_thumbSelectedDrawable)
    mThumbCheckedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_thumbCheckedDrawable)
    mThumbStrokeColor = mTypedArray.getColor(R.styleable.RadiusSwitch_rv_thumbStrokeColor, -1)
    mThumbStrokePressedColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_thumbStrokePressedColor,
      mThumbStrokeColor
    )
    mThumbStrokeDisabledColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_thumbStrokeDisabledColor,
      mThumbStrokeColor
    )
    mThumbStrokeSelectedColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_thumbStrokeSelectedColor,
      mThumbStrokeColor
    )
    mThumbStrokeCheckedColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_thumbStrokeCheckedColor,
      mThumbStrokeColor
    )
    mThumbStrokeWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_thumbStrokeWidth, 0)
    mThumbRadius = mTypedArray.getDimension(R.styleable.RadiusSwitch_rv_thumbRadius, 0f)
    mTrackDrawableWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_trackDrawableWidth, -1)
    mTrackDrawableHeight =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_trackDrawableHeight, -1)
    mTrackDrawable = mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_trackDrawable)
    mTrackPressedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_trackPressedDrawable)
    mTrackDisabledDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_trackDisabledDrawable)
    mTrackSelectedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_trackSelectedDrawable)
    mTrackCheckedDrawable =
      mTypedArray.getDrawable(R.styleable.RadiusSwitch_rv_trackCheckedDrawable)
    mTrackStrokeColor = mTypedArray.getColor(R.styleable.RadiusSwitch_rv_trackStrokeColor, -1)
    mTrackStrokePressedColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_trackStrokePressedColor,
      mTrackStrokeColor
    )
    mTrackStrokeDisabledColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_trackStrokeDisabledColor,
      mTrackStrokeColor
    )
    mTrackStrokeSelectedColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_trackStrokeSelectedColor,
      mTrackStrokeColor
    )
    mTrackStrokeCheckedColor = mTypedArray.getColor(
      R.styleable.RadiusSwitch_rv_trackStrokeCheckedColor,
      mTrackStrokeColor
    )
    mTrackStrokeWidth =
      mTypedArray.getDimensionPixelSize(R.styleable.RadiusSwitch_rv_trackStrokeWidth, 0)
    mTrackRadius = mTypedArray.getDimension(R.styleable.RadiusSwitch_rv_trackRadius, 0f)
    mThumbDrawable =
      if (mThumbDrawable == null) ColorDrawable(Color.TRANSPARENT) else mThumbDrawable
    mThumbPressedDrawable =
      if (mTrackPressedDrawable == null) mThumbDrawable else mThumbPressedDrawable
    mThumbDisabledDrawable =
      if (mThumbDisabledDrawable == null) mThumbDrawable else mThumbDisabledDrawable
    mThumbSelectedDrawable =
      if (mThumbSelectedDrawable == null) mThumbDrawable else mThumbSelectedDrawable
    mThumbCheckedDrawable =
      if (mThumbCheckedDrawable == null) mThumbDrawable else mThumbCheckedDrawable
    mTrackDrawable =
      if (mTrackDrawable == null) ColorDrawable(Color.TRANSPARENT) else mTrackDrawable
    mTrackPressedDrawable =
      if (mTrackPressedDrawable == null) mTrackDrawable else mTrackPressedDrawable
    mTrackDisabledDrawable =
      if (mTrackDisabledDrawable == null) mTrackDrawable else mTrackDisabledDrawable
    mTrackSelectedDrawable =
      if (mTrackSelectedDrawable == null) mTrackDrawable else mTrackSelectedDrawable
    mTrackCheckedDrawable =
      if (mTrackCheckedDrawable == null) mTrackDrawable else mTrackCheckedDrawable
    super.initAttributes(context, attrs)
  }

  override fun init() {
    setSwitchDrawable()
    super.init()
  }

  /**
   * 设置滑块宽度
   *
   * @param width
   * @return
   */
  fun setThumbDrawableWidth(width: Int): RadiusSwitchDelegate {
    mThumbDrawableWidth = width
    return this
  }

  /**
   * 设置滑块高度
   *
   * @param height
   * @return
   */
  fun setThumbDrawableHeight(height: Int): RadiusSwitchDelegate {
    mThumbDrawableHeight = height
    return this
  }

  /**
   * 设置滑块默认状态drawable
   *
   * @param drawable
   * @return
   */
  fun setThumbDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mThumbDrawable = drawable
    return this
  }

  fun setThumbDrawable(resId: Int): RadiusSwitchDelegate {
    return setThumbDrawable(getDrawable(resId))
  }

  /**
   * 设置滑块按下状态drawable
   *
   * @param drawable
   * @return
   */
  fun setThumbPressedDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mThumbPressedDrawable = drawable
    return this
  }

  fun setThumbPressedDrawable(resId: Int): RadiusSwitchDelegate {
    return setThumbPressedDrawable(getDrawable(resId))
  }

  fun setThumbDisabledDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mThumbDisabledDrawable = drawable
    return this
  }

  fun setThumbDisabledDrawable(resId: Int): RadiusSwitchDelegate {
    return setThumbDisabledDrawable(getDrawable(resId))
  }

  fun setThumbSelectedDrawable(thumbSelectedDrawable: Drawable?): RadiusSwitchDelegate {
    mThumbSelectedDrawable = thumbSelectedDrawable
    return this
  }

  fun setThumbSelectedDrawable(resId: Int): RadiusSwitchDelegate {
    return setThumbSelectedDrawable(getDrawable(resId))
  }

  /**
   * 设置滑块checked状态drawable
   *
   * @param drawable
   * @return
   */
  fun setThumbCheckedDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mThumbCheckedDrawable = drawable
    return this
  }

  fun setThumbCheckedDrawable(resId: Int): RadiusSwitchDelegate {
    return setThumbCheckedDrawable(getDrawable(resId))
  }

  /**
   * 设置滑块边框颜色
   *
   * @param color
   * @return
   */
  fun setThumbStrokeColor(color: Int): RadiusSwitchDelegate {
    mThumbStrokeColor = color
    return this
  }

  fun setThumbStrokePressedColor(color: Int): RadiusSwitchDelegate {
    mThumbStrokePressedColor = color
    return this
  }

  fun setThumbStrokeDisabledColor(color: Int): RadiusSwitchDelegate {
    mThumbStrokeDisabledColor = color
    return this
  }

  fun setThumbStrokeSelectedColor(color: Int): RadiusSwitchDelegate {
    mThumbStrokeSelectedColor = color
    return this
  }

  /**
   * 设置滑块边框选中状态颜色
   *
   * @param color
   * @return
   */
  fun setThumbStrokeCheckedColor(color: Int): RadiusSwitchDelegate {
    mThumbStrokeCheckedColor = color
    return this
  }

  /**
   * 设置滑块边框宽度
   *
   * @param width
   * @return
   */
  fun setThumbStrokeWidth(width: Int): RadiusSwitchDelegate {
    mThumbStrokeWidth = width
    return this
  }

  /**
   * 设置边框圆角弧度
   *
   * @param radius
   * @return
   */
  fun setThumbRadius(radius: Float): RadiusSwitchDelegate {
    mThumbRadius = radius
    return this
  }

  /**
   * 设置轨道宽度
   *
   * @param width
   * @return
   */
  fun setTrackDrawableWidth(width: Int): RadiusSwitchDelegate {
    mTrackDrawableWidth = width
    return this
  }

  /**
   * 设置轨道高度
   *
   * @param height
   * @return
   */
  fun setTrackDrawableHeight(height: Int): RadiusSwitchDelegate {
    mTrackDrawableHeight = height
    return this
  }

  /**
   * 设置轨道默认drawable
   *
   * @param drawable
   * @return
   */
  fun setTrackDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mTrackDrawable = drawable
    return this
  }

  fun setTrackDrawable(resId: Int): RadiusSwitchDelegate {
    return setTrackDrawable(getDrawable(resId))
  }

  fun setTrackPressedDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mTrackPressedDrawable = drawable
    return this
  }

  fun setTrackPressedDrawable(resId: Int): RadiusSwitchDelegate {
    return setTrackPressedDrawable(getDrawable(resId))
  }

  fun setTrackDisabledDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mTrackDisabledDrawable = drawable
    return this
  }

  fun setTrackDisabledDrawable(resId: Int): RadiusSwitchDelegate {
    return setTrackDisabledDrawable(getDrawable(resId))
  }

  fun setTrackSelectedDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mTrackSelectedDrawable = drawable
    return this
  }

  fun setTrackSelectedDrawable(resId: Int): RadiusSwitchDelegate {
    return setTrackSelectedDrawable(getDrawable(resId))
  }

  /**
   * 设置轨道checked状态drawable
   *
   * @param drawable
   * @return
   */
  fun setTrackCheckedDrawable(drawable: Drawable?): RadiusSwitchDelegate {
    mTrackCheckedDrawable = drawable
    return this
  }

  fun setTrackCheckedDrawable(resId: Int): RadiusSwitchDelegate {
    return setTrackCheckedDrawable(getDrawable(resId))
  }

  /**
   * 设置轨道边框颜色
   *
   * @param color
   * @return
   */
  fun setTrackStrokeColor(color: Int): RadiusSwitchDelegate {
    mTrackStrokeColor = color
    return this
  }

  fun setTrackStrokePressedColor(color: Int): RadiusSwitchDelegate {
    mTrackStrokePressedColor = color
    return this
  }

  fun setTrackStrokeDisabledColor(color: Int): RadiusSwitchDelegate {
    mTrackStrokeDisabledColor = color
    return this
  }

  fun setTrackStrokeSelectedColor(color: Int): RadiusSwitchDelegate {
    mTrackStrokeSelectedColor = color
    return this
  }

  /**
   * 设置轨道边框checked状态颜色
   *
   * @param color
   * @return
   */
  fun setTrackStrokeCheckedColor(color: Int): RadiusSwitchDelegate {
    mTrackStrokeCheckedColor = color
    return this
  }

  /**
   * 设置轨道边框宽度
   *
   * @param width
   * @return
   */
  fun setTrackStrokeWidth(width: Int): RadiusSwitchDelegate {
    mTrackStrokeWidth = width
    return this
  }

  /**
   * 设置轨道圆角弧度
   *
   * @param radius
   * @return
   */
  fun setTrackRadius(radius: Float): RadiusSwitchDelegate {
    mTrackRadius = radius
    return this
  }

  /**
   * 设置Drawable相关属性
   */
  private fun setSwitchDrawable() {
    mSwitch = mView as Switch?
    if (mStateThumbDrawable == null) {
      mStateThumbDrawable = StateListDrawable()
    }
    if (mStateTrackDrawable == null) {
      mStateTrackDrawable = StateListDrawable()
    }
    mStateThumbDrawable!!.addState(
      intArrayOf(mStateChecked),
      getStateDrawable(
        mThumbCheckedDrawable,
        mThumbRadius,
        mThumbDrawableWidth,
        mThumbDrawableHeight,
        mThumbStrokeWidth,
        mThumbStrokeCheckedColor
      )
    )
    mStateThumbDrawable!!.addState(
      intArrayOf(mStateSelected),
      getStateDrawable(
        mThumbSelectedDrawable,
        mThumbRadius,
        mThumbDrawableWidth,
        mThumbDrawableHeight,
        mThumbStrokeWidth,
        mThumbStrokeSelectedColor
      )
    )
    mStateThumbDrawable!!.addState(
      intArrayOf(mStatePressed),
      getStateDrawable(
        mThumbPressedDrawable,
        mThumbRadius,
        mThumbDrawableWidth,
        mThumbDrawableHeight,
        mThumbStrokeWidth,
        mThumbStrokePressedColor
      )
    )
    mStateThumbDrawable!!.addState(
      intArrayOf(mStateDisabled),
      getStateDrawable(
        mThumbDisabledDrawable,
        mThumbRadius,
        mThumbDrawableWidth,
        mThumbDrawableHeight,
        mThumbStrokeWidth,
        mThumbStrokeDisabledColor
      )
    )
    mStateThumbDrawable!!.addState(
      intArrayOf(),
      getStateDrawable(
        mThumbDrawable,
        mThumbRadius,
        mThumbDrawableWidth,
        mThumbDrawableHeight,
        mThumbStrokeWidth,
        mThumbStrokeColor
      )
    )
    mTrackDrawableHeight = 0
    mStateTrackDrawable!!.addState(
      intArrayOf(mStateChecked),
      getStateDrawable(
        mTrackCheckedDrawable,
        mTrackRadius,
        mTrackDrawableWidth,
        mTrackDrawableHeight,
        mTrackStrokeWidth,
        mTrackStrokeCheckedColor
      )
    )
    mStateTrackDrawable!!.addState(
      intArrayOf(mStateSelected),
      getStateDrawable(
        mTrackSelectedDrawable,
        mTrackRadius,
        mTrackDrawableWidth,
        mTrackDrawableHeight,
        mTrackStrokeWidth,
        mTrackStrokeSelectedColor
      )
    )
    mStateTrackDrawable!!.addState(
      intArrayOf(mStatePressed),
      getStateDrawable(
        mTrackPressedDrawable,
        mTrackRadius,
        mTrackDrawableWidth,
        mTrackDrawableHeight,
        mTrackStrokeWidth,
        mTrackStrokePressedColor
      )
    )
    mStateTrackDrawable!!.addState(
      intArrayOf(mStateDisabled),
      getStateDrawable(
        mTrackDisabledDrawable,
        mTrackRadius,
        mTrackDrawableWidth,
        mTrackDrawableHeight,
        mTrackStrokeWidth,
        mTrackStrokeDisabledColor
      )
    )
    mStateTrackDrawable!!.addState(
      intArrayOf(),
      getStateDrawable(
        mTrackDrawable,
        mTrackRadius,
        mTrackDrawableWidth,
        mTrackDrawableHeight,
        mTrackStrokeWidth,
        mTrackStrokeColor
      )
    )
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      mSwitch!!.thumbDrawable = mStateThumbDrawable
      mSwitch!!.trackDrawable = mStateTrackDrawable
    }
  }

  protected fun getStateDrawable(
    drawable: Drawable?,
    radius: Float,
    width: Int,
    height: Int,
    strokeWidth: Int,
    strokeColor: Int,
  ): Drawable? {
    var drawable = drawable
    drawable = getStateDrawable(drawable, radius, width, height)
    if (drawable is GradientDrawable) {
      val gradientDrawable: GradientDrawable? = drawable as GradientDrawable?
      gradientDrawable!!.setStroke(strokeWidth, strokeColor)
    }
    return drawable
  }
}